part of _;

extension on SettingsPage {
  void controller_showSelectLanguage() {
    final items = [null, zh, en];
    yShowSelectBuilder("切换语言".yLocale(), true, items.map((e) => getLanguageTitle(e)).toList(), (index, action) {
      YLocaleUtils.updateAppLocale(items[index]);
    });
  }

  void controller_showUpdatePassword() {
    final textController_password = TextEditingController(), textController_password_new = TextEditingController();
    yShowStatefulDialog(
      dismissible: false,
      alertBuilder: (pageContext, dialogContext, statefulContext, setDialogState) {
        return AlertDialog(
          content: Column(
            mainAxisSize: MainAxisSize.min,
            crossAxisAlignment: CrossAxisAlignment.end,
            children: [
              TextFormField(
                controller: textController_password,
                obscureText: true,
                decoration: InputDecoration(hintText: "请输入旧密码"),
              ),
              TextFormField(
                controller: textController_password_new,
                obscureText: true,
                decoration: InputDecoration(hintText: "请输入新密码"),
              ),
              YSpacer(height: 30),
              Text("仅限密码注册用户修改", style: TextStyle(fontSize: 12, color: Colors.red)),
            ],
          ),
          actions: [
            YButton(child: Text("取消"), onTap: () => yDismissDialog_unDismissible()),
            YButton(
              child: Text("修改"),
              onTap: () {
                final password = textController_password.text;
                if (password.isEmpty) {
                  yToast("请输入旧密码");
                  return;
                }
                final password_new = textController_password_new.text;
                if (password_new.isEmpty) {
                  yToast("请输入新密码");
                  return;
                }
                AppUtils.check_account(
                  yPage,
                  password,
                  password_new: password_new,
                  onSuccess: (userModel) {
                    yToast("密码修改成功");
                    yDismissDialog_unDismissible();
                  },
                );
              },
            ),
          ],
        );
      },
    );
  }

  void controller_loadCache() {
    String formatSize(double value) {
      int index = 0;
      while (value > 1024) {
        index++;
        value = value / 1024;
      }
      return "${value.toStringAsFixed(2)} ${['B', 'K', 'M', 'G'][index]}";
    }

    FileUtils.getCacheSize().then((value) {
      cache = value <= 0 ? "" : formatSize(value.toDouble());
      setState(() {});
    });
  }
}
